
{mfdeclre.i }
/*
define new shared variable vendor          like po_vend.
define new shared variable rdate           like prh_rcp_date.
define new shared variable rdate1          like prh_rcp_date.
define new shared variable invoiced_amt    as decimal format "->>>>>>>>>>9.99<<<" .

rdate = date(4,1,2007).
rdate1 = date(12,12,2007).
vendor = "S27010".
*/

define input parameter vendor          like po_vend.
define input parameter rdate           like prh_rcp_date.
define input parameter rdate1          like prh_rcp_date.
define output parameter invoiced_amt   as decimal format "->>>>>>>>>>9.99<<<" .


define variable mc-error-number like msg_nbr no-undo.
define variable vouchered_qty like pvo_vouchered_qty no-undo.
define variable last_voucher like pvo_last_voucher no-undo.
define variable ers_status like pvo_ers_status no-undo.
define variable ex_rate like prh_ex_rate no-undo.
define variable ex_rate2 like prh_ex_rate2 no-undo.
define variable project like pvo_project no-undo.
define variable RECEIVER as character initial "07" no-undo.
define variable desc1 as character no-undo.
define variable tax_amt as decimal format "->>>>>>>>>>9.99<<<" INITIAL 0 no-undo.
define variable std_tax as decimal format "->>>>>>>>>>9.99<<<" INITIAL 0 no-undo.
define variable voucheredTax as decimal no-undo.
define variable voucher_yes as character initial "" no-undo.
define variable voucher like pvo_last_voucher no-undo.

define new shared variable rndmthd         like rnd_rnd_mthd.
define new shared variable oldcurr         like prh_curr.
define new shared variable sel_inv         like mfc_logical initial yes.
define new shared variable sel_sub         like mfc_logical initial yes.
define new shared variable sel_mem         like mfc_logical initial no.
define new shared variable use_tot         like mfc_logical initial no.

define new shared variable inv_only        like mfc_logical initial yes.         

define new shared variable show_sub        like mfc_logical initial yes.
define new shared variable std_ext         as decimal format "->>>>>>>>>>9.99<<<" .
define new shared variable pur_ext         as decimal format "->>>>>>>>>>9.99<<<" .
define new shared variable std_cost        as decimal format "->>>>>>>9.99<<<"    .
define new shared variable std_var         as DECIMAL format "->>>>>>>9.99<<<"    .
define new shared variable base_rpt        like po_curr.
define new shared variable base_cost       as decimal format "->>>>>>>>9.99<<<"   .
define new shared variable disp_curr       as CHARACTER format "x(1)" label "C".
define new shared variable descname        as character format "x(16)".
define new shared variable qty_open        as DECIMAL format "->>>>>>9.9<<<<<<" .
define new shared variable last_vend       like prh_vend.
define new shared variable exdrate         like exr_rate.
define new shared variable exdrate2        like exr_rate.
define new shared variable sort_by_desc    like glt_desc no-undo.
define new shared variable valid_mnemonic  like mfc_logical no-undo.
define new shared variable sort_by         as CHARACTER format "x(4)" no-undo.
define new shared variable sort_by_code    like sort_by init "1".

define new shared variable fname           like lngd_dataset initial "poporp07.p"  no-undo.
define new shared variable fr_ps_nbr       like prh_ps_nbr .
define new shared variable to_ps_nbr       like prh_ps_nbr .
define new shared variable last_rcp        like prh_rcp_date.

define variable poders as character format "x(2)" no-undo.
define variable ers-only as logical initial no no-undo.
define variable base_std_cost as decimal.
find first gl_ctrl no-lock no-error.

for each prh_hist where (prh_rcp_date >= rdate and prh_rcp_date <= rdate1) and (prh_vend = vendor)
   and ((prh_type = "" and sel_inv = yes) or (prh_type = "S" and sel_sub = yes) or (prh_type <> "" and prh_type <> "S" and sel_mem = yes))
   and (can-find (first pvo_mstr where pvo_lc_charge = "" and pvo_internal_ref_type = "07" and pvo_internal_ref = prh_receiver and pvo_line = prh_line and pvo_last_voucher <> ""))
   and (base_rpt = "" or base_rpt = prh_curr) no-lock :

    /*
   assign
      voucheredTax = 0
      tax_amt = 0.

   for each tx2d_det
      where tx2d_ref = prh_receiver
      and  tx2d_nbr = prh_nbr
      and  tx2d_line = prh_line
      and  (tx2d_tr_type = "21" or tx2d_tr_type = "25")
      and  tx2d_rcpt_tax_point = yes
      no-lock:
      tax_amt = tax_amt + tx2d_cur_tax_amt.
   end. /*for each tx2d_det*/
   */

   for first pvo_mstr
      where pvo_internal_ref_type = "07"
      and pvo_lc_charge    = ""
      and pvo_internal_ref = prh_receiver
      and pvo_line = prh_line
      no-lock:
      /*
      if inv_only then
         /* Back out the taxes that have been vouchered */
         for each vph_hist where
            vph_pvo_id        = pvo_id  and
            vph_pvod_id_line  = 0       and
            (vph_inv_date >= rdate and
             vph_inv_date <= rdate1) no-lock:

            for each tx2d_det
               where tx2d_ref = vph_ref
               and  tx2d_nbr = prh_receiver
               and  tx2d_line = prh_line
               and  tx2d_tr_type = "22"
               and  tx2d_rcpt_tax_point = yes
               no-lock:
               voucheredTax = voucheredTax + tx2d_cur_tax_amt.
            end. /*for each tx2d_det*/
      end. /* if inv_only = yes then */
      */
   end.

   /* if inv_only = yes then tax_amt = voucheredTax. */

   if available (pvo_mstr) then
      assign
         ex_rate  = pvo_ex_rate
         ex_rate2 = pvo_ex_rate2
       /* project  = pvo_project
         voucher  = pvo_last_voucher */ .
   else
      assign
         ex_rate  = prh_ex_rate
         ex_rate2 = prh_ex_rate2
          /* project  = ""
         voucher  = "" */ .

   if (oldcurr <> prh_curr) or (oldcurr = "") then do:

      if prh_curr = gl_base_curr then
         rndmthd = gl_rnd_mthd.
      else do:
         /* GET ROUNDING METHOD FROM CURRENCY MASTER */
         {gprunp.i "mcpl" "p" "mc-get-rnd-mthd"
            "(input prh_curr,
              output rndmthd,
              output mc-error-number)"}
      end.
      oldcurr = prh_curr.
   end.

   do :
       /*
      /* PRH_PUR_STD IS IN BASE CURRENCY */
      std_cost = prh_pur_std.
      if prh_type = ""
      then
         std_cost = prh_pur_std - prh_ovh_std.
      if use_tot = yes or prh_type = "S"  then std_cost = prh_pur_std.

      /* SAVE THE BASE CURRENCY STANDARD UNIT COST */
      base_std_cost = std_cost.
      /* IF REPORT IS NOT IN BASE THEN CONVERT THE STD UNIT COST */
      /* TO DOCUMENT CURRENCY */
      if base_rpt <> ""
         and prh_curr <> base_curr then
      do:

         /* CONVERT FROM BASE TO FOREIGN CURRENCY */
         {gprunp.i "mcpl" "p" "mc-curr-conv"
            "(input base_curr,
              input prh_curr,
              input ex_rate2,
              input ex_rate,
              input std_cost,
              input false, /* DO NOT ROUND */
              output std_cost,
              output mc-error-number)"}.
      end. /* IF BASE_RPT <> "" AND PRH_CURR <> BASE_CURR */

      /* ALWAYS BEGIN WITH THE DOCUMENT CURRENCY UNIT COST */
      base_cost = prh_curr_amt.
      disp_curr = "".

      /* NO NEED FOR CONVERSION OF BASE COST */

      if base_rpt = ""
         and prh_curr <> base_curr then do:
         /* IF BASE RPT, SET BASE_COST TO BASE FOR DISPLAY ONLY */
         base_cost = prh_pur_cost.
      end.
      */
      if inv_only = yes then
      do:
         /* DETERMINE THE VALUE FOR vouchered_qty */
         {gprun.i ""appvoinv.p""
            "(input """",
              input RECEIVER,
              input prh_receiver,
              input prh_line,
              input prh_ps_nbr,
              output vouchered_qty,
              output last_voucher,
              output ers_status)"}

            qty_open = vouchered_qty * prh_um_conv.
      end.
      else qty_open = prh_rcvd * prh_um_conv.

       /*
      /* CALCULATE STD_EXT USING THE BASE_STD_COST */
      std_ext = base_std_cost * qty_open.

      /* ROUND PER BASE CURRENCY ROUND METHOD */

      {gprunp.i "mcpl" "p" "mc-curr-rnd"
         "(input-output std_ext,
           input rndmthd,
           output mc-error-number)"}

      /* CONVERT STD_EXT AND PUR_EXT TO DOCUMENT CURR IF NECESSARY */
      if base_rpt <> ""
         and prh_curr <> base_curr
      then do:

         /* CONVERT FROM BASE TO FOREIGN CURRENCY */
         {gprunp.i "mcpl" "p" "mc-curr-conv"
            "(input base_curr,
              input prh_curr,
              input ex_rate2,
              input ex_rate,
              input std_ext,
              input true, /* DO ROUND */
              output std_ext,
              output mc-error-number)"}.
      end.

        /* zyf0417 CALCULATE STD_TAX USING THE php__dec02 the average tax rate in ptp */
      FIND FIRST ptp_det where ptp_part = prh_part no-lock no-error.
      IF AVAILABLE ptp_det THEN std_tax = std_ext * ptp__dec02.
        */

      /* CALCULATE PUR_EXT USING THE DOCUMENT CURR COST */
      pur_ext = prh_curr_amt * qty_open.
      
      /* ROUND PER DOC CURRENCY ROUND METHOD */
      {gprunp.i "mcpl" "p" "mc-curr-rnd"
         "(input-output pur_ext,
           input rndmthd,
           output mc-error-number)"}
      
      /* IF REPORT IN BASE, CONVERT EXTENDED AMT TO BASE */
      if base_rpt = "" and prh_curr <> base_curr
      then do:

         /* CONVERT FROM FOREIGN TO BASE CURRENCY */
         {gprunp.i "mcpl" "p" "mc-curr-conv"
            "(input prh_curr,
              input base_curr,
              input ex_rate,
              input ex_rate2,
              input pur_ext,
              input true, /* DO ROUND */
              output pur_ext,
              output mc-error-number)"}.
      end.

         /*
      {gprunp.i "mcpl" "p" "mc-curr-rnd"
         "(input-output tax_amt,
           input rndmthd,
           output mc-error-number)"}
      
      /* CONVERT PUR EXT AMT TO BASE IF NECESSARY */
      if base_rpt = ""
         and prh_curr <> base_curr then do:

         /* CONVERT FROM FOREIGN TO BASE CURRENCY */
         {gprunp.i "mcpl" "p" "mc-curr-conv"
            "(input prh_curr,
              input base_curr,
              input ex_rate,
              input ex_rate2,
              input tax_amt,
              input true, /* DO ROUND */
              output tax_amt,
              output mc-error-number)"}.
      end.

      std_var = pur_ext - std_ext - tax_amt.
      if prh_type <> "" and prh_type <> "S" then do:
         std_cost = 0.
         std_ext  = 0.
         std_var  = 0.
      end.
      */

      accumulate pur_ext (total).
      /*
      accumulate std_ext (total).
      accumulate std_var (total).
      accumulate std_tax (total).
      accumulate tax_amt (total).
      */
   end. /* DO WITH */
end.

invoiced_amt = accum total pur_ext.
